package com.sikaryofficial.backend.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.sikaryofficial.backend.domain.entity.ArticleViews;
import com.sikaryofficial.backend.mapper.ArticleViewsMapper;
import com.sikaryofficial.backend.service.IArticleViewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 文章查阅量Service业务层处理
 *
 * @author qinjinyuan
 * @date 2024-03-14
 */
@Service
public class ArticleViewsServiceImpl extends ServiceImpl<ArticleViewsMapper, ArticleViews> implements IArticleViewsService {
    @Autowired
    private ArticleViewsMapper articleViewsMapper;
    /**
     * 查询文章查阅量
     *
     * @param articleIds 文章查阅量主键
     * @return 文章查阅量
     */
    @Override
    public Map<Long, Long> countArticleViewsByArticleId(List<Long> articleIds) {
        List<ArticleViews> articleViewsList = articleViewsMapper.countArticleViewsByArticleId(articleIds);
        if (CollUtil.isEmpty(articleViewsList)) {
            return Maps.newHashMap();
        }
        return articleViewsList.stream().filter(item-> Objects.nonNull(item.getViewCount()))
                .collect(Collectors.toMap(ArticleViews::getArticleId, ArticleViews::getViewCount, (k1, k2) -> k1));
    }
}
